//-------------------------------------------------------------------------
// File: RegistryValue.cpp
//
// Descr.: - todo -
//
// Author: Stefan Leihkauf
// Date:   8/11/2011
//-------------------------------------------------------------------------
#include "cb_pch.h"
#include "registry/RegistryValue.h"
#include <boost/lexical_cast.hpp>

//-------------------------------------------------------------------------
/**
*/
RegistryValue::RegistryValue(WinHKey hkey, const StdString& name) :
_hkey(hkey), _name(name)
{
	/* It is completly valid for a valuename to be empty. These are the values that are
	   shown as "Default" with RegEdit.
	*/

	UpdateValueData();
}

//-------------------------------------------------------------------------
/**
*/
const StdString& RegistryValue::Name() const
{
	return _name;
}

//-------------------------------------------------------------------------
/**
*/
const RegistryValue::ValueData& RegistryValue::Data() const
{
	return _valueData;
}

//-------------------------------------------------------------------------
/**
*/
RegistryValue::ValueType RegistryValue::ValueKind() const
{
	return _valueData.Type;
}

//-------------------------------------------------------------------------
/**
*/
void RegistryValue::UpdateValueData()
{
	assert(_hkey);

	unsigned long valueType;
	unsigned long szData = 0;

	// first call determines value size
	if(ERROR_SUCCESS == RegGetValue(_hkey, NULL, _name.c_str(), RRF_RT_ANY, &valueType, 0, &szData)) {
		if(szData) {
			char* buffer = new char[szData];
			// second call reads valuedata
			if(ERROR_SUCCESS == RegGetValue(_hkey, NULL, _name.c_str(), RRF_RT_ANY, 0, buffer, &szData)) {
				this->_valueData.Type = (RegistryValue::ValueType)valueType;
				this->_valueData.Size = szData;
				this->_valueData.Data.reset(buffer);
			} else
				delete [] buffer;
		}
	}
}